package com.yanqu.road.dao.impl.activity.jianchuanghua;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jianchuanghua.ChuangHuaUserData;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class ChuangHuaUserDataDaoImpl extends TempDataObjectDao<ChuangHuaUserData> {

    public static String getChuangHuaUserDataSql = "select * from t_u_activity_jian_chuang_hua where userId=? and activityId=?";

    public ChuangHuaUserData getChuangHuaUserData(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getChuangHuaUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateChuangHuaUserDataSql = "update t_u_activity_jian_chuang_hua set score=?,chuangHuaList=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(ChuangHuaUserData chuangHuaUserData) {
        chuangHuaUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, chuangHuaUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(chuangHuaUserData.getChuangHuaProperty())));
        para.put(i++, new DbParameter(Types.BIGINT, chuangHuaUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, chuangHuaUserData.getActivityId()));
        if (getDb().executeNoneQuery(updateChuangHuaUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChuangHuaUserDataSql = "insert into t_u_activity_jian_chuang_hua(userId,activityId,score,chuangHuaList) values (?,?,?,?)";

    @Override
    protected boolean addData(ChuangHuaUserData chuangHuaUserData) {
        chuangHuaUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, chuangHuaUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, chuangHuaUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, chuangHuaUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(chuangHuaUserData.getChuangHuaProperty())));
        if (getDb().executeNoneQuery(insertChuangHuaUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ChuangHuaUserData parseData(ResultSet rs) throws SQLException {
        ChuangHuaUserData chuangHuaUserData = new ChuangHuaUserData();
        chuangHuaUserData.setUserId(rs.getLong("userId"));
        chuangHuaUserData.setActivityId(rs.getInt("activityId"));
        chuangHuaUserData.setScore(rs.getInt("score"));
        String chuangHuaList = rs.getString("chuangHuaList");
        chuangHuaUserData.setChuangHuaProperty(StringUtils.isNullOrEmpty(chuangHuaList) ? new Property() : PropertyHelper.parseStringToProperty(chuangHuaList));
        return chuangHuaUserData;
    }

}
